package com.ehotting.edsta.mqbd.signal.api.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONValidator;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ehotting.eaf.core.StringHelper;
import com.ehotting.eaf.netty.domain.DataAble;
import com.ehotting.eaf.netty.domain.Request;
import com.ehotting.mqbd.api.bean.MQBaseData;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.enums.ProtocolTypeEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.edsta.mqbd.signal.api.domain.netty.CallSimulateRequest;
import org.springframework.util.Assert;

public class MqSignalTcpData extends MQBaseData<DataAble,DataAble> {

    @JSONField(serialize = false)
    private DataAble request;
    @JSONField(serialize = false)
    private DataAble response;



    public MqSignalTcpData() {
        super();
        this.setProtocolType(ProtocolTypeEnum.TCP);
    }

    public MqSignalTcpData(TopicEnum topic, String consumerHost, String localHost, String agentId, String module, String msg) {
        super( topic, consumerHost,  localHost,  agentId,  module,  msg);
        this.setProtocolType(ProtocolTypeEnum.TCP);
    }

    public MqSignalTcpData(TopicEnum topic, MqApplicationContext mqApplicationContext, String agentId, String module, String msg) {
        super( topic, mqApplicationContext.getConsumerHost(),  mqApplicationContext.getLocalHost(),  agentId,  module,  msg);
        this.setProtocolType(ProtocolTypeEnum.TCP);
    }

    public static MqSignalTcpData create(TopicEnum topic, String consumerHost, String localHost, String agentId, String module, String msg){
        Assert.notNull(topic,"topic is null");
        return new MqSignalTcpData( topic, consumerHost,  localHost,  agentId,  module,  msg);
    }

    public static MqSignalTcpData create(TopicEnum topic, MqApplicationContext mqApplicationContext, String agentId, String msg){
        Assert.notNull(topic,"topic is null");
        return new MqSignalTcpData( topic, mqApplicationContext,  agentId,  mqApplicationContext.getModule(),  msg);
    }

    public DataAble getRequest() {
        if(StringHelper.hasText(requestString) && JSONValidator.from(requestString).validate()){
            this.request = (Request) JSON.parseObject(requestString,this.getRequestClazz());
        }
        return request;
    }

    public void setRequest(DataAble request) {
        if(request!=null) {
            this.requestString = JSON.toJSONString(request, SerializerFeature.DisableCircularReferenceDetect);
            this.requestClazz = request.getClass();
        }
        this.request = request;
    }

    public DataAble getResponse() {
        if(StringHelper.hasText(responseString) && JSONValidator.from(responseString).validate()) {
            this.response = (Request) JSON.parseObject(responseString, this.getResponseClazz());
        }
        return response;
    }

    public void setResponse(DataAble response) {
        if(response!=null){
            this.responseString = JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
            this.responseClazz = response.getClass();
        }
        this.response = response;
    }


    public static void main(String[] args) {

        CallSimulateRequest re = new CallSimulateRequest();

        re.setTdrData("111111111111");

         MqSignalTcpData data = MqSignalTcpData.create(TopicEnum.BLOCK,
                "11",
                "222",
                "111",
                "33",
                "112222");
        data.setRequest(re);

        String aa = JSON.toJSONString(data);
        System.out.println(aa);
        MqSignalTcpData bb = JSON.parseObject(aa,MqSignalTcpData.class);
        CallSimulateRequest ss = (CallSimulateRequest)bb.getRequest();
        System.out.println(ss.getTdrData());
    }
}
